
export function readFile(type = "DataURL",multi = false ) {
  let input = document.createElement("input");
  input.id = "file-reader";
  input.type = "file";
  input.hidden = true;

  return new Promise((resovle, reject) => {
    input.onchange = function (e) {
      // console.log(e.target.files)

      if (!this.multi) {
        let file = e.target.files[0];
        var reader = new FileReader();
        
        if (type === "DataURL") {
          reader.readAsDataURL(file);
        } else {
          reader.readAsText(file);
        }

        reader.onload = function () {
            resovle({
                fileName:file.name,
                data:reader.result,
                file:file
            })
        };
      }
    };

    document.body.appendChild(input);
    input.click();
  });
}


export function dataURL2Image(dataURL){
      return new Promise((resovle,reject)=>{
        const image=new Image();
        image.src=dataURL
        image.setAttribute('crossOrigin', 'anonymous');
        image.onload = function(){
            resovle(image)
        }

      })
}